package com.huawei.java.alogrithm;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @program: CodeCraft-2022
 * @description:
 * @author: hzy
 * @create: 2022-03-19 15:40
 **/
public class ResultCompute {

    static Map<String, Integer> result = new HashMap<>();

    static Map<String, List<Integer>> list = new HashMap<>();

    static String path = "./src/output/data.txt";

    public static void init() {
        File file = new File(path);
        if (file.exists()) {
            file.delete();
        }
        result.clear();
        list.clear();
    }

    public static void store(Map<String, Map<String, Integer>> allocationMaps) {
        for (Map.Entry<String, Map<String, Integer>> allocation : allocationMaps.entrySet()) {
            for (Map.Entry<String, Integer> value : allocation.getValue().entrySet()) {
                String key = value.getKey();
                if (!result.containsKey(key)) {
                    result.put(key, 0);
                }
                result.put(key, result.get(key) + value.getValue());
            }
        }
        return;
    }

    public static void compute() {
        StringBuilder clientAllocationInfo = new StringBuilder();

        for (Map.Entry<String, Integer> r : result.entrySet()) {
            System.out.print(r.getKey() + " " + r.getValue() + ",");
            clientAllocationInfo.append(r.getKey() + " " + r.getValue() + ",");
            if (!list.containsKey(r.getKey())) {
                list.put(r.getKey(), new ArrayList<>());
            }
            List l = list.get(r.getKey());
            l.add(r.getValue());
            list.put(r.getKey(), l);
        }
        System.out.println();
        clientAllocationInfo.append("\n");
        result.clear();
    }

    public static int sort(Integer size) {
        int allCost = 0;
        StringBuilder clientAllocationInfo = new StringBuilder();
        clientAllocationInfo.append("\n\n\n\nsort\n");
        for (Map.Entry<String, List<Integer>> e : list.entrySet()) {
            System.out.print(e.getKey() + " ");
            clientAllocationInfo.append(e.getKey() + " ");
            List<Integer> l = e.getValue();
            int s = size - l.size();
            for (int i = 0; i < s; i++) {
                l.add(0);
            }
            Collections.sort(l);

            for (int i = 0; i < l.size(); i++) {
                System.out.print(l.get(i) + " ");
                clientAllocationInfo.append(l.get(i) + " ");
            }
            System.out.println();
            clientAllocationInfo.append("\n");
            // todo 改一下
            int ceil = (int) Math.ceil(size * 0.95);
            System.out.println(e.getKey() + " 95% cost: " + l.get(ceil - 1));


            allCost += l.get(ceil - 1);
        }
        System.out.println("allcost: " + allCost);
        clientAllocationInfo.append("allcost: " + allCost + "\n");

        return allCost;
    }

}
